package com.example.networklibrary;


import com.example.networklibrary.entity.Header;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public abstract class BaseObserver<T> implements Observer<ResponseModel<T>> {

    @Override
    public final void onNext(@NonNull ResponseModel<T> result) {
        try {
            if (Header.Code.CodeSuccess.equals(result.getHeader().getRe_code())) {
                onSuccess(result.getBody());
            } else {
                onFailure(new Exception(result.getHeader().getRe_msg()), result.getHeader().getRe_msg());
            }
        } catch (Exception e) {
            onFailure(new Exception("数据解析出错"), "数据解析出错");
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        onFailure(e, RxExceptionUtil.exceptionHandler(e));
    }


    @Override
    public void onComplete() {

    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {

    }

    public abstract void onSuccess(T result);

    public abstract void onFailure(Throwable e, String errorMsg);
}